<?php
/**
 * @file
 * @package mileage
 * @version $Id$
**/

if (!defined('XOOPS_ROOT_PATH')) {
	exit;
}

require_once MILEAGE_TRUST_PATH . '/class/AbstractFilterForm.class.php';

define('MILEAGE_REC_SORT_KEY_REC_ID',       1);
define('MILEAGE_REC_SORT_KEY_POINT',        2);
define('MILEAGE_REC_SORT_KEY_MILE_ID',      3);
define('MILEAGE_REC_SORT_KEY_UID',          4);
define('MILEAGE_REC_SORT_KEY_REG_UNIXTIME', 5);

define('MILEAGE_REC_SORT_KEY_DEFAULT', MILEAGE_REC_SORT_KEY_REG_UNIXTIME);// REG_UNIXTIME

/**
 * Mileage_Admin_MileFilterForm
**/
class Mileage_LoginFilterForm extends Mileage_AbstractFilterForm
{
	public $year  = NULL;
	public $month = NULL;
	public $uid = NULL;
	private $_rDirname = NULL;

	/**
	 * @var  string[]
	 * 
	 * @public
	**/
	var $mSortKeys = array(
		MILEAGE_REC_SORT_KEY_REC_ID       => 'rec_id',
		MILEAGE_REC_SORT_KEY_POINT        => 'point',
		MILEAGE_REC_SORT_KEY_MILE_ID      => 'mile_id',
		MILEAGE_REC_SORT_KEY_UID          => 'uid',
		MILEAGE_REC_SORT_KEY_REG_UNIXTIME => 'reg_unixtime',
	);

	/**
	 * getDefaultSortKey
	 * 
	 * @param   void
	 * 
	 * @return  void
	**/
	public function getDefaultSortKey()
	{
		return MILEAGE_REC_SORT_KEY_DEFAULT;
	}

	/**
	 * fetch
	 * 
	 * @param   void
	 * 
	 * @return  void
	**/
	public function fetch()
	{
		parent::fetch();

		$root =& XCube_Root::getSingleton();

		$asset = NULL;
		XCube_DelegateUtils::call('Module.mileage.Global.Event.GetAssetManager', new XCube_Ref($asset), $this->_rDirname);
		$handler =& $asset->getObject('handler', 'mile');
		$mileObj =& $handler->getObjects(new Criteria('url', 'login'));
		if (count($mileObj)==1 && is_object($mileObj[0])) {
			$this->_mCriteria->add(new Criteria('mile_id', $mileObj[0]->get('mile_id')));
		}

		// uid
		if ($root->mContext->mXoopsUser->isAdmin() && ($value = $root->mContext->mRequest->getRequest('uid')) !== null) {
			$this->uid = $value;
		} else {
			$this->uid = $root->mContext->mXoopsUser->get('uid');
		}
		$this->_mCriteria->add(new Criteria('uid', $this->uid));

		// ym
		$this->year = date('Y');
		$this->month = date('n');
		if (($ym_value = $root->mContext->mRequest->getRequest('ym')) !== null) {
			list($temp_year, $temp_month) = explode('-', $ym_value);
			if (($temp_year * 10000 + $temp_month * 100) <= ($this->year * 10000 + $this->month * 100)) {
				$this->year = intval($temp_year);
				$this->month = intval($temp_month);
			}
		}
		$starttime = mktime(0,0,0, $this->month, 1, $this->year);
		$days_in_mon = date('t', $starttime);
		// reg_unixtime
		$endtime = mktime(23,59,59, $this->month, $days_in_mon, $this->year);
		$this->_mCriteria->add(new Criteria('reg_unixtime', $starttime, '>='));
		$this->_mCriteria->add(new Criteria('reg_unixtime', $endtime, '<='));

		$this->_mCriteria->addSort($this->getSort(), $this->getOrder());
	}

	public function setDirname($dirname)
	{
		$this->_rDirname = $dirname;
	}
}

